package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.mapper.UserMapper;
import com.example.demo.model.entity.UserEntity;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,UserEntity> implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public boolean login(UserEntity userEntity, HttpServletRequest request) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",userEntity.getUserName());
        HttpSession session = request.getSession();
        UserEntity user  = userMapper.selectOne(queryWrapper);
        if(user == null || !user.getPassword().equals(userEntity.getPassword())){
            return false;
        }
        session.setAttribute("user",user);
        return true;
    }
}
